package org.bitmaps.cutBitmap.view
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.utils.ByteArray;
	
	import org.bitmaps.cutBitmap.tool.DrawArrow;
	import org.bitmaps.cutBitmap.tool.DrawEllipse;
	import org.bitmaps.cutBitmap.tool.DrawRect;
	import org.bitmaps.event.BmpEvent;
	import org.images.JPGEncoder;
	import org.utils.DisplayObjectUtil;
	
	/**
	 * 截屏面板
	 * @author anjue
	 */
	public class CutPanel extends Sprite
	{
		public static const CUT_COMPLETE:String = "cut_complete";
		public static const CUT_EXIT:String = "CUT_EXIT";
		
		private var panel_w:uint = 300;
		private var panel_h:uint = 300;
		
		
		/**横向触点的个数**/
		private var w_spot_num:uint = 3;
		
		/**纵向触点的个数**/
		private var h_spot_num:uint = 3;
		
		private var _container:Sprite;
		private var _bg:Sprite;
		private var _mc_hand:Sprite;
		private var _toolPanel:DrawToolPanel;
		private var _txt_w_h:TextField;
		private var currentDraw:DrawRect;
		private var current_draw_color:uint = 0;
		
		private var drawVec:Vector.<DrawRect>;
		private var spotVec:Vector.<CutTouchSpot>;
		
		public function CutPanel($container:Sprite)
		{
			_container = $container;
			initBg();
			initSpot();
		}
		
		private function initBg():void
		{
			_bg = new Sprite();
			_bg.graphics.beginFill(0xFFFFFF,0.1);
			_bg.graphics.lineStyle(1,0);
			_bg.graphics.drawRect(0,0,panel_w,panel_h);
			_bg.addEventListener(MouseEvent.MOUSE_DOWN,bgDown);
			_bg.doubleClickEnabled = true;
			_bg.addEventListener(MouseEvent.DOUBLE_CLICK,bgDoubleclick);
			_bg.graphics.endFill();
			this.addChild(_bg);
			
			_mc_hand = new Sprite();
			_mc_hand.graphics.beginFill(0xFF0000);
			_mc_hand.graphics.drawRect(0,0,10,10);
			_mc_hand.graphics.endFill();
			this.addChild(_mc_hand);
			_mc_hand.visible = false;
			
			_toolPanel = new DrawToolPanel();
			_toolPanel.drawFun = drawFun;
			_toolPanel.undoFun = undoRect;
			_toolPanel.completeFun = completeFun;
			_toolPanel.colorFun = colorFun;
			_toolPanel.exitFun = exitFun;
			_toolPanel.x = panel_w-_toolPanel.width;
			_toolPanel.y = panel_h + 2;
			this.addChild(_toolPanel);
			
			_txt_w_h = new TextField();
			_txt_w_h.width = 58;
			_txt_w_h.height = 16;
			_txt_w_h.mouseEnabled = false;
			_txt_w_h.textColor = 0xFFFFFF;
			_txt_w_h.background = true;
			_txt_w_h.backgroundColor = 0x010101;
			_txt_w_h.text = panel_w + "*" + panel_h;
			this.addChild(_txt_w_h);
			
			drawVec = new Vector.<DrawRect>();
		}
		
		private function update(bg_alpha:Number = 0.3):void
		{
			panel_w = spotVec[7].x-spotVec[0].x;
			panel_h = spotVec[7].y-spotVec[0].y;
			_bg.graphics.clear();
			_bg.graphics.beginFill(0xFFFFFF,bg_alpha);
			_bg.graphics.lineStyle(1,0);
			_bg.graphics.drawRect(spotVec[0].x,spotVec[0].y,panel_w,panel_h);
			_bg.graphics.endFill();
			updateSpot();
			if(localToGlobal(new Point(spotVec[0].x,spotVec[0].y)).y<16)
			{
				_txt_w_h.y = spotVec[0].y;
			}else
			{
				_txt_w_h.y = spotVec[0].y-16;
			}
			_txt_w_h.x = spotVec[0].x;
			_txt_w_h.text = panel_w + "*" + panel_h;
		}
		
		private function updateSpot():void
		{
			var per_w:uint = w_spot_num-1;
			var per_h:uint = h_spot_num-1;
			var i:uint = 0;
			var len:uint = spotVec.length;
			for(i;i<len;i++)
			{
				spotVec[i].x = spotVec[0].x + spotVec[i].index_x*(panel_w/per_w);
				spotVec[i].y = spotVec[0].y + spotVec[i].index_y*(panel_h/per_w);
			}
		}
		
		private function drawFun(type:uint = 1):void
		{
			_bg.removeEventListener(MouseEvent.MOUSE_DOWN,bgDown);
			if(!currentDraw || currentDraw.type!=type)
			{
				if(currentDraw)
				{
					currentDraw.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
					currentDraw.destroy();
				}
				addDraw(type);
			}
		}
		
		/**
		 * 增加一个绘画  
		 * @param type 1.画矩形  2.画椭圆 3.画箭头
		 */
		public function addDraw(type:uint = 1):void
		{
			var rec:DrawRect;
			switch(type)
			{
				case DrawToolPanel.DRAW_RECT:
					rec = new DrawRect(_bg.getRect(_container));
					break;
				case DrawToolPanel.DRAW_ELLIPSE:
					rec = new DrawEllipse(_bg.getRect(_container));
					break;
				case DrawToolPanel.DRAW_ARROW:
					rec = new DrawArrow(_bg.getRect(_container));
					break;
				default:
					throw "指定绘制类型错误，请检查后再试";
					break;
			}
			rec.color = current_draw_color;
			rec.addEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
			_container.addChild(rec);
			currentDraw = rec;
		}
		
		private function drawComplete(e:Event):void
		{
			var rec:DrawRect = e.currentTarget as DrawRect;
			rec.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
			drawVec.push(rec);
			addDraw(rec.type);
		}
		
		public function clearCurrentDraw():void
		{
			if(currentDraw)
			{
				currentDraw.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
				currentDraw.destroy();
			}
		}
		
		private function undoRect():void
		{
			if(drawVec.length>0)
			{
				var item:DrawRect = drawVec.pop();
				item.destroy();
			}
		}
		
		private function completeFun():void
		{
			var bmd:BitmapData = new BitmapData(_bg.width,_bg.height,true,0);
			var rec:Rectangle = _bg.getRect(this.parent);
			var matrix:Matrix = new Matrix();
			matrix.translate(-rec.x,-rec.y);
			this.visible = false;
			bmd.draw(_container,matrix);
			this.visible = true;
			var by:ByteArray = new JPGEncoder().encode(bmd);
			var eve:BmpEvent = new BmpEvent(CUT_COMPLETE);
			eve.data = by;
			dispatchEvent(eve);
		}
		
		private function colorFun($color:uint):void
		{
			current_draw_color = $color;
			if(currentDraw)
			{
				currentDraw.color = $color;
			}
		}
		
		private function exitFun():void
		{
			var eve:BmpEvent = new BmpEvent(CUT_EXIT);
			dispatchEvent(eve);
		}
		
		private function initSpot():void
		{
			spotVec = new Vector.<CutTouchSpot>();
			var per_w:uint = w_spot_num-1;
			var per_h:uint = h_spot_num-1;
			var i:uint = 0;
			var j:uint = 0;
			var index:uint = 0;
			for(i;i<3;i++)
			{
				j = 0;
				for(j;j<3;j++)
				{
					if(i!=0 && i!=per_w && j!=0 && j!=per_h)
					{
						continue;
					}
					var _spot:CutTouchSpot = new CutTouchSpot();
					_spot.index = index;
					_spot.addEventListener(CutTouchSpot.SPOT_MOVE,spotMove);
					_spot.addEventListener(CutTouchSpot.SPOT_DOWN,spotDown);
					_spot.addEventListener(CutTouchSpot.SPOT_UP,spotUp);
					_spot.x = (panel_w/per_w)*j;
					_spot.y = (panel_h/per_h)*i;
					_spot.index_x = j;
					_spot.index_y = i;
					_spot.original_x = _spot.x;
					_spot.original_y = _spot.y;
					_spot.spotList = spotVec;
					this.addChild(_spot);
					spotVec.push(_spot);
					index++;
				}
			}
		}
		
		private function spotDown(e:Event):void
		{
			_toolPanel.visible = false;
			_txt_w_h.visible = true;
		}
		
		private function spotUp(e:Event):void
		{
			_toolPanel.visible = true;
			update(0.1);
			updateToolPosition();
		}
		
		private function spotMove(e:Event):void
		{
			var target:CutTouchSpot = e.currentTarget as CutTouchSpot;
			if(target.index_x == 2 && target.index_y == 0)
			{
				spotVec[0].y = spotVec[0].original_y+(target.y-target.original_y);
				spotVec[7].x = spotVec[7].original_x+(target.x-target.original_x);
			}else if((target.index_x == 0 && target.index_y == 2))
			{
				spotVec[0].x = spotVec[0].original_x+(target.x-target.original_x);
				spotVec[7].y = spotVec[7].original_y+(target.y-target.original_y);
			}else if(target.index_x == 0)
			{
				spotVec[0].x = spotVec[0].original_x+(target.x-target.original_x);
			}else if(target.index_y == 0)
			{
				spotVec[0].y = spotVec[0].original_y+(target.y-target.original_y);
			}else if(target.index_x == 2)
			{
				spotVec[7].x = spotVec[7].original_x+(target.x-target.original_x);
			}else if(target.index_y == 2)
			{
				spotVec[7].y = spotVec[7].original_y+(target.y-target.original_y);
			}
			update();
		}
		
		private function bgDown(e:MouseEvent):void
		{
			_toolPanel.visible = false;
			var rect:Rectangle = DisplayObjectUtil.limitDisplayInRectangle(_bg,new Rectangle(0,0,stage.stageWidth,stage.stageHeight));
			this.startDrag(false,rect);
			stage.addEventListener(MouseEvent.MOUSE_UP,bgUp);
			_bg.addEventListener(MouseEvent.MOUSE_MOVE,bgMove);
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			
			_mc_hand.visible = true;
			_mc_hand.x = this.mouseX;
			_mc_hand.y = this.mouseY;
			Mouse.hide();
			
		}
		
		private function bgMove(e:MouseEvent):void
		{
			_mc_hand.x = this.mouseX;
			_mc_hand.y = this.mouseY;
		}
		
		private function bgUp(e:MouseEvent):void
		{
			_toolPanel.visible = true;
			this.removeEventListener(Event.ENTER_FRAME,enterFrame);
			_bg.removeEventListener(MouseEvent.MOUSE_MOVE,bgMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,bgUp);
			this.stopDrag();
			updateToolPosition();
			_mc_hand.visible = false;
			Mouse.show();
		}
		
		private function bgDoubleclick(e:MouseEvent):void
		{
			completeFun();
		}
		
		private function enterFrame(e:Event):void
		{
			if(localToGlobal(new Point(spotVec[0].x,spotVec[0].y)).y<16)
			{
				_txt_w_h.y = spotVec[0].y;
			}else
			{
				_txt_w_h.y = spotVec[0].y-16;
			}
		}
		
		/**
		 * 更新工具条位置
		 */
		private function updateToolPosition():void
		{
			var _w:uint = spotVec[7].x;
			var _h:uint = spotVec[7].y;
			if(localToGlobal(new Point(_w,_h)).y>stage.stageHeight-24)
			{
				_toolPanel.x = _w-_toolPanel.width-5;
				_toolPanel.y = _h-24;
			}else
			{
				_toolPanel.x = _w-_toolPanel.width-5;
				_toolPanel.y = _h;
			}
		}
		
		public function destroy():void
		{
			this.removeEventListener(Event.ENTER_FRAME,enterFrame);
			stage.removeEventListener(MouseEvent.MOUSE_UP,bgUp);
			_bg.removeEventListener(MouseEvent.MOUSE_MOVE,bgMove);
			_bg.removeEventListener(MouseEvent.DOUBLE_CLICK,bgDoubleclick);
			while(drawVec.length>0)
			{
				var item:DrawRect = drawVec.pop();
				item.destroy();
			}
			while(spotVec.length>0)
			{
				var spot:CutTouchSpot = spotVec.pop();
				spot.removeEventListener(CutTouchSpot.SPOT_MOVE,spotMove);
				spot.destroy();
			}
			if(currentDraw)
			{
				currentDraw.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
				currentDraw.destroy();
			}
			if(_toolPanel)
			{
				_toolPanel.destroy();
				_toolPanel = null;
			}
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}